package com.example.myapplication;

import android.graphics.Color;
import android.os.Bundle;
import android.widget.TextView;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.content.ContextCompat;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

import com.github.mikephil.charting.charts.CandleStickChart;
import com.github.mikephil.charting.components.Description;
import com.github.mikephil.charting.components.XAxis;
import com.github.mikephil.charting.components.YAxis;
import com.github.mikephil.charting.data.CandleData;
import com.github.mikephil.charting.data.CandleDataSet;
import com.github.mikephil.charting.data.CandleEntry;
import com.github.mikephil.charting.formatter.IndexAxisValueFormatter;
import com.github.mikephil.charting.highlight.Highlight;
import com.github.mikephil.charting.listener.OnChartValueSelectedListener;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity implements OnChartValueSelectedListener {

    private CandleStickChart candleStickChart;
    private TextView tvInfo;
    private List<KLineData> kLineDataList;
    private KLineChartStyler chartStyler;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        setContentView(R.layout.activity_main);
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
            Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
            return insets;
        });

        initViews();
        initKLineChart();
        loadKLineData();
    }

    /**
     * 初始化视图
     */
    private void initViews() {
        candleStickChart = findViewById(R.id.candleStickChart);
        tvInfo = findViewById(R.id.tv_info);
        chartStyler = new KLineChartStyler(this);
        
        // 设置图表点击监听器
        candleStickChart.setOnChartValueSelectedListener(this);
    }

    /**
     * 初始化K线图表配置
     */
    private void initKLineChart() {
        // 应用专业样式
        chartStyler.applyProfessionalStyle(candleStickChart);
        
        // 应用亮色主题
        chartStyler.applyLightTheme(candleStickChart);
        
        // 设置动画效果
        chartStyler.setAnimations(candleStickChart);
    }

    /**
     * 加载K线数据
     */
    private void loadKLineData() {
        // 生成示例数据
        kLineDataList = KLineDataGenerator.generateDefaultKLineData();
        
        // 转换为图表数据
        List<CandleEntry> entries = new ArrayList<>();
        List<String> xLabels = new ArrayList<>();
        
        for (int i = 0; i < kLineDataList.size(); i++) {
            KLineData data = kLineDataList.get(i);
            CandleEntry entry = new CandleEntry(
                i,                    // x轴位置
                data.getHigh(),       // 最高价
                data.getLow(),        // 最低价
                data.getOpen(),       // 开盘价
                data.getClose()       // 收盘价
            );
            entries.add(entry);
            xLabels.add(data.getDate());
        }

        // 创建数据集
        CandleDataSet dataSet = chartStyler.createStyledDataSet(entries, "K线数据");
        
        // 设置X轴标签
        XAxis xAxis = candleStickChart.getXAxis();
        xAxis.setValueFormatter(new IndexAxisValueFormatter(xLabels));
        
        // 创建图表数据并设置
        CandleData candleData = new CandleData(dataSet);
        candleStickChart.setData(candleData);
        
        // 刷新图表
        candleStickChart.invalidate();
    }

    /**
     * 图表值选中回调
     */
    @Override
    public void onValueSelected(com.github.mikephil.charting.data.Entry e, Highlight h) {
        if (e instanceof CandleEntry) {
            CandleEntry candleEntry = (CandleEntry) e;
            int index = (int) candleEntry.getX();
            
            if (index >= 0 && index < kLineDataList.size()) {
                KLineData data = kLineDataList.get(index);
                String info = getString(R.string.kline_info_format,
                    data.getDate(),
                    data.getOpen(),
                    data.getHigh(),
                    data.getLow(),
                    data.getClose(),
                    data.getVolume()
                );
                tvInfo.setText(info);
            }
        }
    }

    /**
     * 图表取消选中回调
     */
    @Override
    public void onNothingSelected() {
        tvInfo.setText(R.string.kline_info_default);
    }
}